package com.zmovie.admin.controller.actor;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmovie.admin.constant.Constants;
import com.zmovie.common.error.BusinessException;
import com.zmovie.common.error.EnumBusinessError;
import com.zmovie.common.response.CommonReturnType;
import com.zmovie.common.utils.FileUtils;
import com.zmovie.framework.domain.Actor;
import com.zmovie.framework.service.ActorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * @author 挚爱之夕
 * @version 1.0
 * @implSpec 演员信息管理
 * @since 2023-12-04 15:29
 */
@Controller
@RequestMapping("/actor/info")
@Api(tags = "演员信息管理")
public class ActorController {
    @Autowired
    private ActorService actorService;
    @GetMapping
    @ApiOperation("演员信息页面")
    public String index(Model model){
        //分页查询演员信息
        Page<Actor> actorPage = actorService.pageActor(Integer.valueOf(Constants.PAGE_NO), Integer.valueOf(Constants.PAGE_SIZE), new Actor());
        model.addAttribute(Constants.ACTOR_PAGE, actorPage);
        return "actor/actor_info";
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id获取演员信息")
    @ResponseBody
    public Object getActor(@PathVariable("id") @NotNull Integer id){
        return CommonReturnType.success(actorService.getById(id));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除演员信息")
    @ResponseBody
    public Object deleteActor(@PathVariable("id") @NotNull Integer id){
        //删除图片
        deleteActorFile(id);
        actorService.removeById(id);
        return CommonReturnType.success("删除成功!");
    }
    @PostMapping
    @ApiOperation("添加演员信息")
    @ResponseBody
    public Object addActor(@Validated Actor actor, List<MultipartFile> pictures){
        //保存图片
        List<String> fileNameList = FileUtils.saveFile(pictures, FileUtils.actorPath);
        String jsonStr = JSON.toJSONString(fileNameList);
        actor.setPicture(jsonStr);
        actorService.save(actor);
        return CommonReturnType.success("添加成功!");
    }

    @PutMapping
    @ApiOperation("修改演员信息")
    @ResponseBody
    public Object updateActor(@Validated Actor actor, List<MultipartFile> pictures) throws BusinessException {
        if(actor.getId() == null){
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        //删除原来保存的图片
        deleteActorFile(actor.getId());
        //保存图片
        List<String> fileNameList = FileUtils.saveFile(pictures, FileUtils.actorPath);
        String jsonStr = JSON.toJSONString(fileNameList);
        actor.setPicture(jsonStr);
        actorService.updateById(actor);
        return CommonReturnType.success("修改成功!");
    }
    @DeleteMapping
    @ApiOperation("批量删除演员图片")
    @ResponseBody
    public Object deleteActorBatch(Integer[] id) throws BusinessException {
        //参数校验
        if (id == null || id.length == 0) {
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        //删除保存的图片
        for(Integer i : id){
            deleteActorFile(i);
        }
        actorService.removeByIds(Arrays.asList(id));
        return CommonReturnType.success("批量删除成功!");
    }

    @PostMapping("/search")
    @ApiOperation("搜索")
    public String search(Actor actor, Integer pageNo, Integer pageSize, Model model){
        Page<Actor> actorPage = actorService.pageActor(pageNo, pageSize, actor);
        model.addAttribute(Constants.ACTOR_PAGE, actorPage);
        return "actor/actor_info :: actorList";
    }

    //删除保存的图片
    private void deleteActorFile(Integer actorId){
        try {
            //封面
            String pictureJson = actorService.getById(actorId).getPicture();
            //反序列化
            List<String> fileNameList = JSON.parseArray(pictureJson, String.class);
            FileUtils.delete(fileNameList);
        } catch (BusinessException e) {
            e.printStackTrace();
        }
    }
}
